<template>
  <el-aside class="aside-container">
    <el-menu :default-active="activeIndex" class="el-menu-vertical" background-color="#545c64" text-color="#fff"
      active-text-color="#ffd04b" router @open="handleOpen" @close="handleClose">
      <el-menu-item index="/index">
        <el-icon>
          <House />
        </el-icon>
        <span>首页</span>
      </el-menu-item>

      <el-menu-item index="/user">
        <el-icon>
          <UserFilled />
        </el-icon>
        <span>用户管理</span>
      </el-menu-item>

      <el-menu-item index="/book">
        <el-icon>
          <Reading />
        </el-icon>
        <span>图书管理</span>
      </el-menu-item>

      <el-menu-item index="/borrow">
        <el-icon>
          <List />
        </el-icon>
        <span>借阅管理</span>
      </el-menu-item>

      <el-sub-menu index="settings">
        <template #title>
          <el-icon>
            <Tools />
          </el-icon>
          <span>系统设置</span>
        </template>
        <el-menu-item-group title="用户设置">
          <el-menu-item index="/user/detail">
            <el-icon>
              <User />
            </el-icon>
            <span>用户详情</span>
          </el-menu-item>
        </el-menu-item-group>
        <el-menu-item-group title="系统信息">
          <el-menu-item index="/about">
            <el-icon>
              <InfoFilled />
            </el-icon>
            <span>关于系统</span>
          </el-menu-item>
        </el-menu-item-group>
      </el-sub-menu>
    </el-menu>
  </el-aside>
</template>

<script lang="ts" setup>
import { ref, onMounted } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import {
  House,
  UserFilled,
  Reading,
  List,
  Tools,
  User,
  InfoFilled
} from '@element-plus/icons-vue'

const route = useRoute()
const router = useRouter()
const activeIndex = ref(route.path)

// 监听路由变化更新激活菜单
onMounted(() => {
  // 初始化时设置当前路由对应的菜单项
  activeIndex.value = route.path
})

const handleOpen = (key: string, keyPath: string[]) => {
  console.log('打开菜单:', key, keyPath)
}

const handleClose = (key: string, keyPath: string[]) => {
  console.log('关闭菜单:', key, keyPath)
}
</script>

<style scoped>
.aside-container {
  height: 100vh;
  background-color: #545c64;
}

.el-menu-vertical {
  border-right: none;
}

.el-menu-vertical:not(.el-menu--collapse) {
  width: 200px;
  min-height: 400px;
}

:deep(.el-sub-menu .el-sub-menu__title) {
  padding-left: 20px !important;
}

:deep(.el-menu-item) {
  padding-left: 20px !important;
}

:deep(.el-sub-menu .el-menu-item) {
  padding-left: 40px !important;
}

.el-icon {
  margin-right: 8px;
  vertical-align: middle;
}

/* 菜单项hover效果 */
:deep(.el-menu-item:hover) {
  background-color: #4a4f57 !important;
}

/* 激活菜单项样式 */
:deep(.el-menu-item.is-active) {
  background-color: #434a50 !important;
}

/* 图标样式优化 */
:deep(.el-icon) {
  font-size: 18px;
}

/* 菜单文字样式 */
:deep(.el-menu-item span) {
  font-size: 14px;
}

/* 菜单组标题样式 */
:deep(.el-menu-item-group__title) {
  padding: 7px 0 7px 20px;
  line-height: normal;
  font-size: 12px;
  color: #909399;
}

/* 子菜单样式 */
:deep(.el-sub-menu.is-active .el-sub-menu__title) {
  color: #ffd04b !important;
}
</style>